{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask, request, render_template, redirect,url_for,flash\n",
    "import time\n",
    "import os\n",
    "import sqlite3 # 导入sqlit3库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 文件上传的地址\n",
    "UPLOAD_FOLDER = 'static/vedios'\n",
    "\n",
    "app = Flask(__name__)\n",
    "app.secret_key = '123456'\n",
    "app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  连接到一个数据库名为 records 的数据库,如果存在则直接连接,如果不存在则创建\n",
    "data_base=sqlite3.connect('reocords', check_same_thread=False)\n",
    "# 设置数据库光标,你之后所有对数据库进行的操作都是通过光标来执行的\n",
    "cursor=data_base.cursor()\n",
    "# 创建一个表名为 uploads 的数据库表,如果这个表不存在的话\n",
    "# 后面括号里面的内容为这个表的属性,属性与属性之间用  ,  隔开,属性名与属性类型之间用 空格  隔开,如果不写类型的话,默认       为text类型\n",
    "cursor.execute('create table if not exists uploads(info text)')\n",
    "# 数据库的提交,对数据进行增删改后都需要进行数据库的提交\n",
    "data_base.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getrecords():\n",
    "    cursor.execute('select * from uploads')\n",
    "    results=cursor.fetchall()\n",
    "    records=[]\n",
    "    for r in results:\n",
    "        records.append(eval(r[0]))\n",
    "    return records"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.route('/', methods=['GET', 'POST'])\n",
    "def index():\n",
    "    if request.method == 'GET':\n",
    "        records=getrecords()\n",
    "        # print(records)\n",
    "        return render_template('index.html',records=records[::-1])\n",
    "\n",
    "    else:#post方法\n",
    "        scon = request.form.get('scon')\n",
    "        target = []\n",
    "        # 搜索云端是否有该文件，罗列所有相关的\n",
    "        records=getrecords()\n",
    "        for record in records:\n",
    "            # print(record['filename'])\n",
    "            if scon in record['filename']:\n",
    "                target.append(record)\n",
    "        if target==[]:#没有找到相关文件\n",
    "            flash('没有找到相关文件！')\n",
    "            return render_template('index.html',records=records[::-1])\n",
    "        else:\n",
    "            return render_template('index.html',records=target[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.route('/upload', methods=['GET', 'POST'])\n",
    "def upload_file():\n",
    "    if request.method == 'POST':\n",
    "        file = request.files['file']\n",
    "        if file:\n",
    "            filename = file.filename.rsplit('.', 1)[0]\n",
    "            filetype = file.filename.rsplit('.', 1)[1]\n",
    "            filename = filename.replace(' ','') # 将文件名中的空格去除\n",
    "            filename = filename.replace('.','-') # 去除文件中的.\n",
    "            filename = filename+'.'+filetype\n",
    "            # 解决命名冲突的问题\n",
    "            records=getrecords()\n",
    "            for record in records:\n",
    "                if filename==record['filename']:\n",
    "                    flash('该文件已经存在！')\n",
    "                    return render_template('upload.html')\n",
    "            # ----------\n",
    "            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))\n",
    "            date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())\n",
    "            record = {\"filename\": filename, \"date\": date}\n",
    "            # print(record)\n",
    "            # 更新本地记录\n",
    "            cursor.execute('insert into uploads (info) VALUES (\\\"%s\\\")'%(record))\n",
    "            data_base.commit()\n",
    "            return redirect(url_for('index'))\n",
    "    return render_template('upload.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.route(\"/player/<filename>\", methods=['GET'])\n",
    "def player(filename):\n",
    "    path = \"../\"+app.config['UPLOAD_FOLDER']+\"/\"+filename\n",
    "    return render_template('player.html',path=path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "if __name__ == '__main__':\n",
    "    app.run(host='0.0.0.0',threaded=True,port=5000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
